<?php
/**
 * Punny - The most easy-to-use PHP MVC framework
 * 
 * Copyright (c) 2010  
 *
 * @author email :lx.xin@qq.com
 */
 require('BaseController.php');
 class Admin_ClassesController extends Admin_BaseController {
	var $classes;
	function init(){
		parent::init();
		$this->classes =new Classes();
	}
	/*
	 *获取分类列表
	 */
	function indexAction (){
		$numPerPage = 10;
		$getPage = $this->_request->getParam('page');
		$page = empty($getPage)?'1':$getPage;
		//抓回所有要显示的记录
		$array=$this->classes->getClasses();
		//factory出一个分页并且设置当前页码与每页显数
		$paginator = Zend_Paginator::factory($array);
		$paginator->setCurrentPageNumber($page)
				  ->setItemCountPerPage($numPerPage);
         //把分页显示到view
		$this->view->paginator = $paginator;		
		echo $this->view->render('classes.html');
	}

	/*
	 *添加分类
	 */
	 function addAction (){
		if(isset($_POST['add'])){
			$filter=array(
				'*'=>'StringTrim'					
			);
			$validate=array(
					'classname'=>array(
						array('StringLength',1,100),
						Zend_Filter_Input::MESSAGES=>array(array(
							Zend_Validate_StringLength::INVALID=>"请输入一个合法的字符串",
							Zend_Validate_StringLength::TOO_SHORT=>"请输入字符长度为1-100",
							Zend_Validate_StringLength::TOO_LONG=>"请输入字符长度为1-100"	
					))
					),
					'remark'=>array('NotEmpty','message'=>array(
						Zend_Validate_NotEmpty::IS_EMPTY=>"不能为空"
					))
				);
			$input= new Zend_Filter_Input($filter,$validate,$this->getRequest()->getPost());
			if($input->isValid()){
				if($this->classes->check($input->getEscaped('classname'))){
					$this->classes->addClass($input->getEscaped());
					$this->view->url=$this->_request->getBaseUrl()."/admin/classes/index/"; 
					$this->view->msg="添加分类成功";
					echo $this->view->render('success.html');
				}
				else{
					$this->view->url=$this->_request->getBaseUrl()."/admin/classes/add/";
					$this->view->msg="分类名称已经存在";
					echo $this->view->render('error.html');
				}
				
			}
			else{
				$this->view->errors=$input->getMessages();
				$this->view->url=$this->_request->getBaseUrl()."/admin/classes/add/";
				$this->view->msg="添加失败";
				echo $this->view->render('error.html');
			}
		}
		else{
			$this->view->x='add';
			echo $this->view->render('classes.html');
		}
	 }
	 /*
	  *编辑分类
	  */
	  function editAction (){
			if(isset($_POST['edit'])){
				$id=$this->_request->getPost('id');				
				$data['classname']=$this->_request->getPost('classname');
				$data['remark']=$this->_request->getPost('remark');
				$this->classes->updateClass($id,$data);
				$this->view->url=$this->_request->getBaseUrl()."/admin/classes/";
				$this->view->msg="编辑成功";
				echo $this->view->render('success.html');
			}
			else{
				$id=$this->_request->get('id');
				if($id){
					$data=$this->classes->queryClass($id);
					$this->view->id=$data['id'];
					$this->view->classname=$data['classname'];
					$this->view->remark=$data['remark'];
					$this->view->x='edit';
					echo $this->view->render('classes.html');
				}
				else{
					$this->view->url=$this->_request->getBaseUrl()."/admin/classes/add/";
					$this->view->msg="编辑的数据不存在";
					echo $this->view->render('error.html');
				}
			}
	  }
	  /*
	   *删除分类
	   */
	   function delAction (){
			$id=$this->_request->get('id');
			if($id>0){
				$this->classes->delClass($id);
				$this->view->url=$this->_request->getBaseUrl()."/admin/classes/";
				$this->view->msg="删除成功";
				echo $this->view->render('success.html');
			}
			else{
				$this->view->url=$this->_request->getBaseUrl()."/admin/classes/";
				$this->view->msg="删除数据不存在";
				echo $this->view->render('error.html');
			}
	   }
 }
 ?>